package com.pkk.penguin.frame.know.java.interview.year2021.atguigu_20210226.thread;

import org.junit.Test;

/**
 * 线程题
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-03-16 09:44
 **/
public class ThreadSimple1 {


  static Object synchronizedObj = new Object();


  /**
   * 多线程问题：使用两个线程交替打印 1到9 即最后输出类似 1 1 2 2 3 3。。。9 9 （给出基本代码）
   *
   * @return void
   * @Param
   * @author peikunkun
   * @date 2021/3/16 0016 上午 9:45
   * @since
   */
  @Test
  public void Administrator_14_20210316094457_() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_14()_20210316094457】");
    System.out.println("此方法测试描述：【】");

    new Thread(() -> {
      for (int i = 1; i < 10; i++) {
        final int temp = i;
        synchronized (synchronizedObj) {
          synchronizedObj.notify();
          System.out.println(Thread.currentThread().getName() + "->" + temp);
          try {
            synchronizedObj.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }, "A").start();

    Thread.sleep(300);

    new Thread(() -> {
      for (int i = 1; i < 10; i++) {
        final int temp = i;
        synchronized (synchronizedObj) {
          synchronizedObj.notify();
          try {
            System.out.println(Thread.currentThread().getName() + "->" + temp);
            synchronizedObj.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }, "B").start();

  }

}
